﻿using System;
using System.IO;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Support_Upload : UserPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateBind();
        }
    }

    private void DateBind()
    {
        string sqlstr = "SELECT MAX(STARTED) FROM ALARM_TABLE_3G";
        Date3G.Text = "【3G】" + OracleHelper.ExecuteScalar(sqlstr, CommandType.Text, null).ToString();
        sqlstr = "SELECT MAX(ALARM_START_TIME) FROM ALARM_TABLE_2G";
        Date2G.Text = "【2G】" + OracleHelper.ExecuteScalar(sqlstr, CommandType.Text, null).ToString();
    }

    protected void UploadBtn_Click(object sender, EventArgs e)
    {
        if (AlarmInfo.HasFile)
        {            
            DataTable dt = null;
            try
            {                
                using (MemoryStream stream = new MemoryStream(AlarmInfo.FileBytes))
                {
                    int num = 0;
                    dt = ExcelUtils.TranslateToTable(stream, 0);
                    string sqlstr = String.Empty;
                    if (Net.SelectedValue.Equals("2G", StringComparison.InvariantCultureIgnoreCase))
                    {
                        OracleHelper.ExecuteNonQuery("TRUNCATE TABLE ALARM_TABLE_2G_TEMP", CommandType.Text, null);
                        sqlstr = "SELECT FLOW_NUMBER AS \"流水号\",ALARM_ID AS ID,ALARM_NAME AS 名称,ALARM_START_TIME AS 发生时间,ALARM_RECOVER_TIME AS 恢复时间,ALARM_POSITION AS 定位信息,ALARM_SOURCE_NAME AS 告警来源,BSC FROM ALARM_TABLE_2G_TEMP";
                        num = OracleHelper.MultiInsertData(sqlstr, dt);
                        OracleHelper.ExecuteNonQuery("AB_MODIFY_2GCELL_P", CommandType.StoredProcedure, null);
                    }
                    else
                    {
                        OracleHelper.ExecuteNonQuery("TRUNCATE TABLE ALARM_TABLE_3G_TEMP", CommandType.Text, null);
                        sqlstr = "SELECT C_NUMBER AS \"Alarm NUMBER\",ALARM_ID AS \"Alarm ID\",CELL_ID AS OBJECT,STARTED,ALARM_TEXT AS \"ALARM TEXT\",SUPP_TEXT AS \"Supplementary Text\",RNW_OBJECT_NAME AS \"RNW Object Name\",CANCELED FROM ALARM_TABLE_3G_TEMP";
                        num = OracleHelper.MultiInsertData(sqlstr, dt);
                        OracleHelper.ExecuteNonQuery("AB_MODIFY_3GCELL_P", CommandType.StoredProcedure, null);
                    }
                    DateBind();
                    JQueryAlert("导入成功,共导入" + num.ToString() + "条有效告警信息！");
                }
            }
            catch (Exception ex)
            {
                log4net.LogManager.GetLogger(GetType()).Error("数据上传出现错误！", ex);
                throw ex;
            }
        }
        else
        {
            JQueryAlert("请选择上传文件！");
        }
    }
}
